import { defineStore } from 'pinia'
import request from '@/utils/request'
import {
  ApiRes,
  BannerItem,
  NewGoodItem,
  HotGoodItem,
  BrandItem,
  HomeProduct,
  Special
} from '@/types/data'

const useHomeStore = defineStore('home', {
  state() {
    return {
      bannerList: [] as BannerItem[],
      newGoodsList: [] as NewGoodItem[],
      hotGoodsList: [] as HotGoodItem[],
      brandList: [] as BrandItem[],
      productList: [] as HomeProduct[],
      specialList: [] as Special[]
    }
  },
  getters: {},
  actions: {
    // 轮播图数据
    async getBannerList() {
      const res = await request.get<ApiRes<BannerItem[]>>('/home/banner')
      this.bannerList = res.data.result
    },
    // 新鲜好物数据
    async getNewGoodsList() {
      const res = await request.get<ApiRes<NewGoodItem[]>>('/home/new')
      this.newGoodsList = res.data.result
    },
    // 人气推荐数据
    async getHotGoodsList() {
      const res = await request.get<ApiRes<HotGoodItem[]>>('/home/hot')
      this.hotGoodsList = res.data.result
    },
    // 热门品牌数据
    async getBrandList() {
      const res = await request.get<ApiRes<BrandItem[]>>('/home/brand')
      this.brandList = [...res.data.result, ...res.data.result]
    },
    // 商品主体数据
    async getProductList() {
      const res = await request.get<ApiRes<HomeProduct[]>>('/home/goods')
      this.productList = res.data.result
    },
    // 最新专题
    async getSpecialList() {
      const res = await request.get<ApiRes<Special[]>>('/home/special')
      this.specialList = res.data.result
    }
  }
})

export default useHomeStore
